package backend.project.gym_management.controller;

import backend.project.gym_management.pojo.dto.*;
import backend.project.gym_management.pojo.vo.StaffEquipmentPageVO;
import backend.project.gym_management.pojo.vo.StaffRepairPageVO;
import backend.project.gym_management.pojo.vo.StaffUserPageVO;
import backend.project.gym_management.base.response.JsonResult;
import backend.project.gym_management.base.response.StatusCode;
import backend.project.gym_management.service.StaffService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 员工功能模块
 */
@Slf4j
@RestController
@RequestMapping("/v1/staff/")
@Api(tags = "03.员工功能模块")
public class StaffController {
    @Autowired
    private StaffService staffService;

    /**
     * 查询用户信息
     * @param staffQuery
     * @return
     */
    @ApiOperation("查询用户信息")
    @ApiOperationSupport(order = 10)
    @GetMapping("user/query")
    public JsonResult selectUser(StaffQuery staffQuery){
        log.debug("开始处理查询用户信息请求，参数：{}", staffQuery);
        StaffUserPageVO users = staffService.selectUserByNameOrUid(staffQuery);
        log.debug("查询用户信息成功，结果：{}", users);
        if(users != null){
            return JsonResult.ok(users);
        }else {
            return new JsonResult(StatusCode.INVALID_PARAMETER, "用户id或用户名不存在");
        }
    }
    /**
     * 修改器械状态
     * @param update
     * @return JsonResult
     */
    @ApiOperation("修改器械状态")
    @ApiOperationSupport(order = 20)
    @PutMapping("/equipment/update")
    public JsonResult updateEquipmentStatus(@RequestBody @Valid StaffUpdateEquipmentParam update){
        log.debug("开始处理修改器械状态请求，参数：{}", update);
        boolean result = staffService.updateStatus(update);
        if (result){
            return JsonResult.ok("状态更新成功");
        }else {
            return new JsonResult(StatusCode.INVALID_PARAMETER, "修改失败,未找到该器械id");
        }
    }

    /**
     * 新增维修记录功能
     * @param add
     * @return JsonResult
     */
    @ApiOperation("新增维修记录")
    @ApiOperationSupport(order = 30)
    @PostMapping("repair/add")
    public JsonResult addRepairRecord(@RequestBody @Valid StaffAddRepairStatus add){
        log.debug("开始处理新增维修记录请求，参数：{}", add);
        boolean result = staffService.addRepairRecord(add);
        if (result){
            return JsonResult.ok("新增成功");
        }else {
            return new JsonResult(StatusCode.INVALID_PARAMETER, "器械id不存在");
        }
    }

    /**
     * 查询所有器械
     * @return JsonResult
     */
    @ApiOperation("查询所有器械")
    @ApiOperationSupport(order = 40)
    @GetMapping("/equipment/all")
    public JsonResult getAllEquipment(@Valid StaffEquipmentQuery query) {
        StaffEquipmentPageVO equipmentList = staffService.getAllEquipmentByPage(query);
        return JsonResult.ok(equipmentList);
    }

    /**
     * 查询所有维修记录
     * @return JsonResult
     */
    @ApiOperation("查询所有维修记录")
    @ApiOperationSupport(order = 50)
    @GetMapping("/repair/all")
    public JsonResult getAllRepairRecords(@Valid StaffRepairQuery query) {
        StaffRepairPageVO records = staffService.getAllRepairsByPage(query);
        return JsonResult.ok(records);
    }

    /**
     * 修改用户头衔
     * @param param
     * @return
     */
    @ApiOperation("修改用户头衔")
    @ApiOperationSupport(order = 60)
    @PostMapping("/user/update/title")
    public JsonResult updateUserTitle(@RequestBody @Valid StaffUpdateUserTitleParam param) {
        log.debug("开始处理修改用户头衔请求，参数：{}", param);
        boolean result = staffService.updateUserTitle(param);
        if (result) {
            return JsonResult.ok("头衔更新成功");
        } else {
            return new JsonResult(StatusCode.INVALID_PARAMETER, "用户不存在");
        }
    }

    /**
     * 添加新用户
     * @param param
     * @return
     */
    @ApiOperation("添加新用户")
    @ApiOperationSupport(order = 70)
    @PostMapping("/user/add")
    public JsonResult addUser(@RequestBody @Valid StaffAddUserParam param) {
        boolean result = staffService.addUser(param);
        if (result) {
            return JsonResult.ok("添加成功");
        } else {
            return new JsonResult(StatusCode.INVALID_PARAMETER, "账号已存在");
        }
    }

}
